package com.colorlight.cloud.complete.entity.enumerate;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.basics.ienum.EnumTrans;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

@Getter
public enum UploadResult implements EnumTrans<CompleteEntity.UploadResult, UploadResult> {
    /**
     * 默认结果
     * 成功上传
     * 上传失败-访问URL失败
     * 失败上传 未知异常
     */
    UploadResultDefault(CompleteEntity.UploadResult.UploadResultDefault),
    successUpload(CompleteEntity.UploadResult.successUpload),
    errorUploadFailedToAccessURL(CompleteEntity.UploadResult.errorUploadFailedToAccessURL),
    errorUpload(CompleteEntity.UploadResult.errorUpload),
    ;

    private final Integer id;
    private final String name;
    private final CompleteEntity.UploadResult uploadResult;

    UploadResult(CompleteEntity.UploadResult uploadResult) {
        this.id = uploadResult.getNumber();
        this.name = uploadResult.name();
        this.uploadResult = uploadResult;
    }

    /**
     * @param uploadResult
     * @return 对应的类型
     */
    public static UploadResult getUploadResultStruct(CompleteEntity.UploadResult uploadResult) {
        Objects.requireNonNull(uploadResult, "uploadResult can not be null.");
        return Arrays.stream(UploadResult.values())
                .filter(e -> e.name.equals(uploadResult.name()))
                .findFirst()
                .orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
    }

    @Override
    public CompleteEntity.UploadResult toProto() {
        return this.uploadResult;
    }

    @Override
    public UploadResult formProto(CompleteEntity.UploadResult uploadResult) {
        return getUploadResultStruct(uploadResult);
    }
}

